package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.Art;
import cn.gjculture.shopping.api.entity.Commodity;
import cn.gjculture.shopping.api.entity.PageInfo;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.ArtService;
import cn.gjculture.shopping.api.util.CommonUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/art")
public class ArtController {

    @Autowired
    ArtService artService;


    @ApiOperation(value = "新增艺术品", notes = "新增艺术品")
    @ApiImplicitParam(name = "art", value = "艺术品对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(String params) {
        Art art= JSONObject.parseObject(params,Art.class);
        Return ret = new Return();
        if(artService.insert(art)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "修改艺术品", notes = "修改艺术品")
    @ApiImplicitParam(name = "art", value = "艺术品对象", required = true, dataType = "Object")
    @PutMapping
    public Return<Art> update(String params){
        Art art= JSONObject.parseObject(params,Art.class);
        Return<Art> ret=new Return<>();
        if(artService.update(art)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "修改艺术品", notes = "修改艺术品")
    @ApiImplicitParam(name = "art", value = "艺术品对象", required = true, dataType = "Object")
    @PutMapping(value = "/updateOther")
    public Return<Art> updateOther(String params){
        Art art= JSONObject.parseObject(params,Art.class);
        Return<Art> ret=new Return<>();
        if(artService.updateOther(art)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "获取艺术品列表", notes = "获取艺术品列表")
    @GetMapping
    public Return<List<Art>> query(Art art, PageInfo pageInfo) {
        Return<List<Art>> ret = new Return<>();
        List<Art> arts = artService.query(art);
        ret.setData(arts);
        if(!CollectionUtils.isEmpty(arts)){
            ret.getCommon().setRet(true);
        }
        return ret;
    }



    @ApiOperation(value = "获取艺术品", notes = "获取艺术品")
    @ApiImplicitParam(name = "artId", value = "艺术品ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{artId}")
    public Return<Art> getById(@PathVariable(value = "artId") String artId) {
        Return<Art> ret = new Return<>();
        Art art = artService.queryById(artId);
        ret.setData(art);
        if(art!=null){
            ret.getCommon().setRet(true);
        }
        return ret;
    }


    @ApiOperation(value = "删除艺术品", notes = "删除艺术品")
    @ApiImplicitParam(name = "artId", value = "艺术品ID", required = true, dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{artId}")
    public Return<Art> delete(@PathVariable(value = "artId") String artId) {
        Return<Art> ret = new Return<>();
        if(artService.delete(artId,ret)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @PostMapping(value = "thumbnail")
    public Return updateThumbnail(String thumbnails, String goodsId){
        artService.updateThumbnail(thumbnails,goodsId);
        return Return.setRet(true);
    }

    @GetMapping(value = "thumbnail/{goodsId}")
    public Return queryThumbnail(@PathVariable(value = "goodsId") String goodsId){
        List<Commodity.Thumbnail> thumbnails= artService.queryThumbnail(goodsId);
        return CommonUtils.setRetForList(thumbnails,new Return<List<Commodity.Thumbnail>>());
    }



}
